package wo.xsys.generator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import wo.common.util.WoJavaDocUtil;
import wo.common.util.WoUtil;
import wo.jpa.reader.entity.WoJPAEntity;
import wo.jpa.reader.util.JPAReaderUtil;

/**
 * wo-ext34sys-sss-web项目代码生成器.
 * @author cailei
 * @date 2018年11月13日
 */
public class XSysGenerator {
	private final static Logger LOG = LogManager.getLogger(XSysGenerator.class);

	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createCode(String projectDir, String entityClass) {
		LOG.info("createCode:rootDir-" + projectDir + ",entityClass-" + entityClass);
		String srcDir = getSrcDir(projectDir);
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		// 创建*Dto类
		JPAReaderUtil.createCode(srcDir, e, "dto", true);
		// 创建*Repository接口
		JPAReaderUtil.createCode(srcDir, e, "repository", false);
		// 创建*Service接口
		JPAReaderUtil.createCode(srcDir, e, "service", false);
		// 创建*ServiceImpl类
		JPAReaderUtil.createCode(srcDir, e, "service.impl", false);
		// 创建*Controller类
		JPAReaderUtil.createCode(srcDir, e, "controller", false);
	}
	
	private static String getSrcDir (String projectDir) {
		return WoUtil.getDirEndWithFileSeparator(projectDir) + "src/main/java/";
	}
	
	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createJs(String projectDir, String entityClass) {
		LOG.info("createCode:rootDir-" + projectDir + ",entityClass-" + entityClass);
		projectDir = WoUtil.getDirEndWithFileSeparator(projectDir);
		String srcDir = getSrcDir(projectDir);
		// 设置java源代码目录
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		String filePath = projectDir + "src/main/webapp/js/app/" + e.getModuleUpperFirstChar() + e.getEntityName() + ".js";
		JPAReaderUtil.createFile(e, "entity.js", filePath);
	}
	
	public static void main(String[] args) {
		// createCode("../wo-ext34sys-sss-web/", "wo.xsys.po.Menu");
		// createJs ("../wo-ext34sys-sss-web/", "wo.xsys.po.Menu");
		JPAReaderUtil.findEntitiesByPackage("../wo-ext34sys-sss-web/", "wo.xsys.po", (projectDir, entityCls) -> {
			createCode(projectDir, entityCls);
			createJs(projectDir, entityCls);
		});
	}
}
